package net.shopxx.dao.impl;

import java.util.List;

import net.shopxx.bean.Pager;
import net.shopxx.bean.Pager.OrderType;
import net.shopxx.dao.RoleDao;
import net.shopxx.entity.Role;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Repository;

/**
 * Dao实现类 - 角色
 */

@Repository
public class RoleDaoImpl extends BaseDaoImpl<Role, String> implements RoleDao {

	// 忽略isSystem=true的对象
	@Override
	public void delete(Role role) {
		if (role.getIsSystem()) {
			return;
		}
		super.delete(role);
	}

	// 忽略isSystem=true的对象
	@Override
	public void delete(String id) {
		Role role = load(id);
		this.delete(role);
	}

	// 忽略isSystem=true的对象
	@Override
	public void delete(String[] ids) {
		if (ids != null && ids.length > 0) {
			for (String id : ids) {
				this.delete(id);
			}
		}
	}


	@SuppressWarnings("unchecked")
	public List<Role> getRootRoleList() {
		String hql = "from Role role where role.parent is null order by role.orderList asc";
		return getSession().createQuery(hql).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Role> getParentRoleList(Role role) {
		String hql = "from Role role where role != ? and role.id in(:ids) order by role.orderList asc";
		String[] ids = role.getPath().split(Role.PATH_SEPARATOR);
		return getSession().createQuery(hql).setParameter(0, role).setParameterList("ids", ids).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Role> getChildrenRoleList(Role role) {
		String hql = "from Role role where role != ? and role.path like ? order by role.orderList asc";
		return getSession().createQuery(hql).setParameter(0, role).setParameter(1, role.getPath() + "%").list();
	}
	
	// 重写方法，保存时计算path值
	@Override
	public String save(Role role) {
		role.setIsSystem(false);
		String id = super.save(role);
		Role parent = role.getParent();
		if (parent != null) {
			String parentPath = parent.getPath();
			role.setPath(parentPath + Role.PATH_SEPARATOR + id);
		} else {
			role.setPath(id);
		}
		super.update(role);
		return id;
	}
	
	// 重写方法，更新时计算path值
	@Override
	public void update(Role role) {
		// 忽略isSystem=true的对象
		if (role.getIsSystem()) {
			return;
		}
		Role parent = role.getParent();
		if (parent != null) {
			String parentPath = parent.getPath();
			role.setPath(parentPath + Role.PATH_SEPARATOR + role.getId());
		} else {
			role.setPath(role.getId());
		}
		
		super.update(role);
	}
	
	// 根据orderList排序
	@SuppressWarnings("unchecked")
	@Override
	public List<Role> getAll() {
		String hql = "from Role role order by role.orderList asc role.createDate desc";
		return getSession().createQuery(hql).list();
	}

	// 根据orderList排序
	@Override
	@SuppressWarnings("unchecked")
	public List<Role> getList(String propertyName, Object value) {
		String hql = "from Role role where role." + propertyName + "=? order by role.orderList asc role.createDate desc";
		return getSession().createQuery(hql).setParameter(0, value).list();
	}
	
	// 根据orderList排序
	@Override
	public Pager findByPager(Pager pager, DetachedCriteria detachedCriteria) {
		if (pager == null) {
			pager = new Pager();
			pager.setOrderBy("orderList");
			pager.setOrderType(OrderType.asc);
		}
		return super.findByPager(pager, detachedCriteria);
	}

	// 根据orderList排序
	@Override
	public Pager findByPager(Pager pager) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Role.class);
		return this.findByPager(pager, detachedCriteria);
	}

}